//
//  SettingViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/11.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "SettingViewController.h"
#import "UpdateUserInfoTableViewCell.h"
#import "ResetPasswordViewController.h"
#import "PrivacyViewController.h"
#import "SettingLoginOutTableViewCell.h"
#import "LoginViewController.h"
#import "BLNavigationController.h"
#import "BLHeader.h"
#define ReuseIdentify2 @"reuse2"
#import <SDWebImage/SDImageCache.h>
#import "JPUSHService.h"


@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource, SettingLoginOutTableViewCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSDictionary *sectionTitle;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


- (void)setupUI {
    self.title = @"设置";
    NSArray *titles = @[@"重置密码", @"用户协议", @"清除缓存"];
    
    self.sectionTitle = @{@"0" : titles};
    [self.tableView registerNib:[UINib nibWithNibName:@"UpdateUserInfoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    [self.tableView registerNib:[UINib nibWithNibName:@"SettingLoginOutTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify2];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return @" ";
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 1) {
        return 1;
    }
    NSArray *titles = [self.sectionTitle objectForKey:[NSString stringWithFormat:@"%li",section]];
    
    return titles.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section != 1){
        UpdateUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
        NSArray *titles = [self.sectionTitle objectForKey:[NSString stringWithFormat:@"%li",indexPath.section]];
        if (indexPath.row == 2){
            CGFloat tmpSize =  [[SDImageCache sharedImageCache] getSize];
            float MBCache = tmpSize/1000/1000;
            
            NSString *clearCacheName = MBCache >= 1 ? [NSString stringWithFormat:@"%.2fMB",MBCache] : [NSString stringWithFormat:@"%.2fKB",MBCache * 1024];
            [cell setupCell:titles[indexPath.row] detailInfo:clearCacheName];
        }else {
            [cell setupCell:titles[indexPath.row] detailInfo:@" "];
        }
        
       
        return cell;
    }
    
    SettingLoginOutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify2];
    cell.delegate = self;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0) {
        if(indexPath.row == 0) {
            ResetPasswordViewController *resetPwd = [[ResetPasswordViewController alloc] init];
            [resetPwd setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:resetPwd animated:YES];
            [resetPwd setHidesBottomBarWhenPushed:NO];
        }else if(indexPath.row == 1) {
            PrivacyViewController *privacy = [[PrivacyViewController alloc] init];
            [privacy setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:privacy animated:YES];
            [privacy setHidesBottomBarWhenPushed:NO];
        }else if(indexPath.row == 2) {
            [self clearChach];
        }
    }
}

- (void)loginOut {
    __weak typeof (self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定退出？" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:@"" forKey:Logined];
        [userDefault setObject:@" "  forKey:USERNAME];
        [userDefault setObject:@" " forKey:USERPWD];
        [userDefault setObject:@" " forKey:USERSIGN];
        [userDefault setObject:@" " forKey:USERADDR];
        [userDefault setObject:@"" forKey:USERCLASS];
        [userDefault setObject:@" " forKey:USERNUM];
        [userDefault setObject:@" " forKey:USERPAY];
        [userDefault setObject:@" " forKey:USERSEX];
        [userDefault setObject:@"" forKey:USERTEL];
        [userDefault setObject:@" " forKey:USERIMG];
        [userDefault synchronize]; 
        BLNavigationController *nav = [[BLNavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
        [weakSelf presentViewController:nav animated:YES completion:nil];
        
        [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
            [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            } seq: 0];
        }];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)clearChach {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" " message:@"将清除所有缓存文件" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    __weak typeof (self) weakSelf = self;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
        
        [[SDImageCache sharedImageCache] clearMemory];//可有可无
        [weakSelf.tableView reloadData];
    }]];
   [self presentViewController:alert animated:YES completion:nil];
}



@end
